
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>选择排序 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="快速排序.html" />
    
    
    <link rel="prev" href="插入排序.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    数据结构（C语言版） 复习总结
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../chapter1.html">
            
                <a href="../chapter1.html">
            
                    
                    第一章 绪论
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../chapter2/chapter2.html">
            
                <a href="../chapter2/chapter2.html">
            
                    
                    第二章 线性表
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../chapter2/顺序表.html">
            
                <a href="../chapter2/顺序表.html">
            
                    
                    线性表的顺序表示和实现
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../chapter2/链表.html">
            
                <a href="../chapter2/链表.html">
            
                    
                    线性表的链式表示和实现
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../chapter3/chapter3.html">
            
                <a href="../chapter3/chapter3.html">
            
                    
                    第三章 栈和队列
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../chapter3/栈.html">
            
                <a href="../chapter3/栈.html">
            
                    
                    抽象数据类型栈的定义和实现
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../chapter3/队列.html">
            
                <a href="../chapter3/队列.html">
            
                    
                    抽象数据类型队列的定义和实现
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    第四章 串
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../chapter4/串类型.html">
            
                <a href="../chapter4/串类型.html">
            
                    
                    串类型的定义
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../chapter4/串的表示和实现.html">
            
                <a href="../chapter4/串的表示和实现.html">
            
                    
                    串的表示和实现
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../chapter4/串的模式匹配算法.html">
            
                <a href="../chapter4/串的模式匹配算法.html">
            
                    
                    串的模式匹配算法
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    第五章 数组和广义表
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../chapter5/数组的定义.html">
            
                <a href="../chapter5/数组的定义.html">
            
                    
                    数组的定义
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../chapter5/矩阵的压缩存储.html">
            
                <a href="../chapter5/矩阵的压缩存储.html">
            
                    
                    矩阵的压缩存储
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../chapter5/广义表.html">
            
                <a href="../chapter5/广义表.html">
            
                    
                    广义表
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" >
            
                <span>
            
                    
                    第六章 树和二叉树
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../chapter6/树.html">
            
                <a href="../chapter6/树.html">
            
                    
                    树的定义与基本术语
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../chapter6/二叉树.html">
            
                <a href="../chapter6/二叉树.html">
            
                    
                    二叉树
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.2.1" data-path="../chapter6/二叉树的二叉链表实现.html">
            
                <a href="../chapter6/二叉树的二叉链表实现.html">
            
                    
                    二叉树的二叉链表实现
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../chapter6/遍历二叉树和线索二叉树.html">
            
                <a href="../chapter6/遍历二叉树和线索二叉树.html">
            
                    
                    遍历二叉树和线索二叉树
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../chapter6/树和森林.html">
            
                <a href="../chapter6/树和森林.html">
            
                    
                    树和森林
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="../chapter6/赫夫曼树.html">
            
                <a href="../chapter6/赫夫曼树.html">
            
                    
                    赫夫曼树
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" >
            
                <span>
            
                    
                    第七章 图
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../chapter7/图.html">
            
                <a href="../chapter7/图.html">
            
                    
                    图的定义
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../chapter7/图的存储结构.html">
            
                <a href="../chapter7/图的存储结构.html">
            
                    
                    图的存储结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../chapter7/图的遍历.html">
            
                <a href="../chapter7/图的遍历.html">
            
                    
                    图的遍历
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="../chapter7/最小生成树.html">
            
                <a href="../chapter7/最小生成树.html">
            
                    
                    最小生成树
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="../chapter7/有向无环图.html">
            
                <a href="../chapter7/有向无环图.html">
            
                    
                    有向无环图的应用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../chapter8/查找.html">
            
                <a href="../chapter8/查找.html">
            
                    
                    第八章 查找
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../chapter8/静态查找表.html">
            
                <a href="../chapter8/静态查找表.html">
            
                    
                    静态查找表
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="../chapter8/动态查找表.html">
            
                <a href="../chapter8/动态查找表.html">
            
                    
                    动态查找表
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" data-path="../chapter8/哈希表.html">
            
                <a href="../chapter8/哈希表.html">
            
                    
                    哈希表
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="内部排序.html">
            
                <a href="内部排序.html">
            
                    
                    第九章 内部排序
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="插入排序.html">
            
                <a href="插入排序.html">
            
                    
                    插入排序
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.10.2" data-path="选择排序.html">
            
                <a href="选择排序.html">
            
                    
                    选择排序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.3" data-path="快速排序.html">
            
                <a href="快速排序.html">
            
                    
                    快速排序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.4" data-path="归并排序和基数排序.html">
            
                <a href="归并排序和基数排序.html">
            
                    
                    归并排序和基数排序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.5" data-path="排序方法的比较.html">
            
                <a href="排序方法的比较.html">
            
                    
                    排序方法的比较
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >选择排序</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="&#x9009;&#x62E9;&#x6392;&#x5E8F;">&#x9009;&#x62E9;&#x6392;&#x5E8F;</h2>
<p><strong>&#x9009;&#x62E9;&#x6392;&#x5E8F;</strong>&#x7684;&#x57FA;&#x672C;&#x601D;&#x60F3;&#x662F;&#xFF1A;&#x6BCF;&#x4E00;&#x8D9F;&#x5728;&#x540E;&#x9762; <em>n-i</em> &#x4E2A;&#x5F85;&#x6392;&#x8BB0;&#x5F55;&#x4E2D;&#x9009;&#x53D6;&#x5173;&#x952E;&#x5B57;&#x6700;&#x5C0F;&#x7684;&#x8BB0;&#x5F55;&#x4F5C;&#x4E3A;&#x6709;&#x5E8F;&#x5E8F;&#x5217;&#x4E2D;&#x7684;&#x7B2C; <em>i</em> &#x4E2A;&#x8BB0;&#x5F55;&#x3002;</p>
<h3 id="&#x7B80;&#x5355;&#x9009;&#x62E9;&#x6392;&#x5E8F;">&#x7B80;&#x5355;&#x9009;&#x62E9;&#x6392;&#x5E8F;</h3>
<p><strong>&#x7B80;&#x5355;&#x9009;&#x62E9;&#x6392;&#x5E8F;</strong>&#x7684;&#x57FA;&#x672C;&#x601D;&#x60F3;&#x662F;&#xFF1A;&#x901A;&#x8FC7; <em>n-i</em> &#x6B21;&#x5173;&#x952E;&#x5B57;&#x95F4;&#x7684;&#x6BD4;&#x8F83;&#xFF0C;&#x4ECE; <em>n-i+1</em> &#x4E2A;&#x8BB0;&#x5F55;&#x4E2D;&#x9009;&#x51FA;<strong>&#x5173;&#x952E;&#x5B57;&#x6700;&#x5C0F;</strong>&#x7684;&#x8BB0;&#x5F55;&#xFF0C;&#x5E76;&#x548C;&#x7B2C; i(1&lt;=i&lt;=n) &#x4E2A;&#x8BB0;&#x5F55;&#x4EA4;&#x6362;&#x4F4D;&#x7F6E;&#x3002;</p>
<pre><code class="lang-C"><span class="hljs-comment">/**
 * &#x5BFB;&#x627E;&#x6700;&#x5C0F;&#x503C;&#x6240;&#x5728;&#x7684;&#x4F4D;&#x7F6E; 
 */</span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">SelectMinKey</span><span class="hljs-params">(SqList L, <span class="hljs-keyword">int</span> i)</span>
</span>{
    <span class="hljs-keyword">int</span> k;
    ElemType Min = L.data[i];
    <span class="hljs-keyword">int</span> MinIndex = i;
    <span class="hljs-keyword">for</span> (k = i+<span class="hljs-number">1</span>; k &lt; L.length; k++)
    {
        <span class="hljs-keyword">if</span> (L.data[k] &lt; Min)
        {
            Min = L.data[k];
            MinIndex = k;
        }
    }
    <span class="hljs-keyword">return</span> MinIndex;
}

<span class="hljs-comment">/**
 * &#x7B80;&#x5355;&#x9009;&#x62E9;&#x6392;&#x5E8F;&#xFF08;&#x5347;&#x5E8F;&#xFF09; 
 */</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">SelectSort</span><span class="hljs-params">(SqList &amp;L)</span>
</span>{
    <span class="hljs-keyword">int</span> i, j;
    ElemType temp;
    <span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i&lt;L.length; i++)
    {
        j = SelectMinKey(L, i); <span class="hljs-comment">// &#x627E;&#x51FA;&#x6700;&#x5C0F;&#x503C;&#x6240;&#x5728;&#x4F4D;&#x7F6E;&#xFF08;&#x964D;&#x5E8F;&#x6392;&#x5217;&#x6539;&#x4E3A;&#x627E;&#x6700;&#x5927;&#x503C;&#xFF09;</span>
        <span class="hljs-keyword">if</span>(i != j)
        {
            temp = L.data[i];
            L.data[i] = L.data[j];
            L.data[j] = temp;
        }
    }
}
</code></pre>
<h3 id="&#x5806;&#x6392;&#x5E8F;">&#x5806;&#x6392;&#x5E8F;</h3>
<p>&#x4EC0;&#x4E48;&#x662F;&#x5806;&#xFF1F;</p>
<p>&#x5982;&#x679C;&#x8BA9;&#x6EE1;&#x8DB3;&#x4EE5;&#x4E0A;&#x6761;&#x4EF6;&#x7684;&#x5E8F;&#x5217; (k<sub>1</sub> , k<sub>2</sub> , &#xB7;&#xB7;&#xB7; , k<sub>n</sub>) &#x987A;&#x6B21;&#x6392;&#x6210;&#x4E00;&#x68F5;&#x5B8C;&#x5168;&#x4E8C;&#x53C9;&#x6811;&#xFF0C;&#x5219;&#x5806;&#x7684;&#x542B;&#x4E49;&#x8868;&#x660E;&#xFF1A;&#x5B8C;&#x5168;&#x4E8C;&#x53C9;&#x6811;&#x4E2D;&#x6240;&#x6709;&#x975E;&#x7EC8;&#x7AEF;&#x7ED3;&#x70B9;&#x7684;&#x503C;&#x5747;&#x4E0D;&#x5927;&#x4E8E;&#xFF08;&#x6216;&#x4E0D;&#x5C0F;&#x4E8E;&#xFF09;&#x5176;&#x5DE6;&#x3001;&#x53F3;&#x5B69;&#x5B50;&#x7ED3;&#x70B9;&#x7684;&#x503C;&#xFF0C;&#x6B64;&#x6811;&#x7684;&#x6839;&#x7ED3;&#x70B9;&#xFF08;&#x5373;&#x5806;&#x9876;&#x5143;&#x7D20;&#xFF09;&#x5FC5;&#x4E3A;&#x5E8F;&#x5217;&#x4E2D; n &#x4E2A;&#x5143;&#x7D20;&#x7684;&#x6700;&#x5C0F;&#xFF08;&#x6216;&#x6700;&#x5927;&#x503C;&#xFF09;&#x3002;</p>
<p>&#x82E5;&#x5728;&#x8F93;&#x51FA;&#x5806;&#x9876;&#x7684;&#x6700;&#x5C0F;&#x503C;&#x4E4B;&#x540E;&#xFF0C;&#x4F7F;&#x5F97;&#x5269;&#x4F59; <em>n-1</em> &#x4E2A;&#x5143;&#x7D20;&#x7684;&#x5E8F;&#x5217;&#x53C8;&#x5EFA;&#x6210;&#x4E00;&#x4E2A;&#x5806;&#xFF0C;&#x5219;&#x5F97;&#x5230; n &#x4E2A;&#x5143;&#x7D20;&#x4E2D;&#x7684;&#x6B21;&#x5C0F;&#x503C;&#x3002;&#x5982;&#x6B64;&#x53CD;&#x590D;&#x6267;&#x884C;&#xFF0C;&#x4FBF;&#x80FD;&#x5F97;&#x5230;&#x4E00;&#x4E2A;&#x6709;&#x5E8F;&#x5E8F;&#x5217;&#xFF0C;&#x8FD9;&#x4E2A;&#x8FC7;&#x7A0B;&#x79F0;&#x4E4B;&#x4E3A;<strong>&#x5806;&#x6392;&#x5E8F;</strong>&#x3002;</p>
<p>&#x5982;&#x4F55;&#x5728;&#x8F93;&#x51FA;&#x5806;&#x9876;&#x5143;&#x7D20;&#x4E4B;&#x540E;&#xFF0C;&#x8C03;&#x6574;&#x5269;&#x4F59;&#x5143;&#x7D20;&#x6210;&#x4E3A;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x5806;&#xFF1F;&#xFF08;&#x8BE6;&#x89C1;&#x53C2;&#x8003;&#x8D44;&#x6599; 280 &#x9875;&#xFF09;</p>
<p>&#x5982;&#x679C;&#x5C06;&#x4E00;&#x4E2A;&#x5E8F;&#x5217;&#x770B;&#x6210;&#x662F;&#x4E00;&#x4E2A;&#x5B8C;&#x5168;&#x4E8C;&#x53C9;&#x6811;&#xFF0C;&#x5219;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x975E;&#x7EC8;&#x7AEF;&#x7ED3;&#x70B9;&#x662F;&#x7B2C; [n/2]&#xFF08;&#x5411;&#x4E0B;&#x53D6;&#x6574;&#xFF09;&#x4E2A;&#x5143;&#x7D20;&#xFF0C;&#x7531;&#x6B64;&#x8FDB;&#x884C;&#x5806;&#x7684;&#x521B;&#x5EFA;&#x548C;&#x7B5B;&#x9009;&#x3002;&#x8F93;&#x51FA;&#x5806;&#x9876;&#x5143;&#x7D20;&#x4E4B;&#x540E;&#xFF0C;&#x4EE5;&#x5806;&#x4E2D;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x4EE3;&#x66FF;&#x4E4B;&#xFF0C;&#x7136;&#x540E;&#x8FDB;&#x884C;&#x7B5B;&#x9009;&#xFF08;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x5373;&#x4E3A;&#x5E8F;&#x5217;&#x4E2D;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#xFF09;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="插入排序.html" class="navigation navigation-prev " aria-label="Previous page: 插入排序">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="快速排序.html" class="navigation navigation-next " aria-label="Next page: 快速排序">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"选择排序","level":"1.10.2","depth":2,"next":{"title":"快速排序","level":"1.10.3","depth":2,"path":"chapter9/快速排序.md","ref":"chapter9/快速排序.md","articles":[]},"previous":{"title":"插入排序","level":"1.10.1","depth":2,"path":"chapter9/插入排序.md","ref":"chapter9/插入排序.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"chapter9/选择排序.md","mtime":"2018-08-30T13:24:06.684Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-09-02T10:05:33.540Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

